﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LKVMLMLib
{
    public class ChoNhanService
    {
        public bool addNewProvideRoom(ProvideRoom pr)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                db.Configuration.ProxyCreationEnabled = false;
                ProvideRoom _pr = new ProvideRoom();
                _pr.UserId = pr.UserId;
                _pr.RoomCode = pr.RoomCode;
                db.ProvideRooms.Add(_pr);
                db.SaveChanges();
                return true;
            }
        }
        public bool addNewRoomCode(RoomCode rc)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                RoomCode _rc = new RoomCode();
                _rc.RoomCode1 = rc.RoomCode1;
                _rc.Status = rc.Status;
                _rc.Use = rc.Use;
                _rc.CreateDate = rc.CreateDate;
                db.RoomCodes.Add(_rc);
                db.SaveChanges();
                return true;
            }
        }
        public bool addNewRevive(ReviveRoom r2)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                ReviveRoom _r2 = new ReviveRoom();
                _r2.UserId = r2.UserId;
                _r2.RoomCode = r2.RoomCode;
                db.ReviveRooms.Add(_r2);
                db.SaveChanges();
                return true;
            }
        }
        public List<ProvideRoom> getListProvideRoom()
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.ProvideRooms.ToList();
            }
        }
        public List<ProvideRoom> getListProvideRoomByRoomCode (string RoomCode){
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.ProvideRooms.Where(r => r.RoomCode == RoomCode).ToList();
            }
        }
        public List<ReviveRoom> getListReviveRoom()
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.ReviveRooms.OrderBy(r=>r.RoomCode).ToList();
            }
        }
        public List<ReviveRoom> getListReviveRoomByRoomCode(string RoomCode)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.ReviveRooms.Where(r => r.RoomCode == RoomCode).ToList();
            }
        }

        public RoomCode getRoomCode()
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.RoomCodes.Where(r=>r.Use==false).OrderBy(r => r.CreateDate).FirstOrDefault();
            }
        }

        public bool addRoomCode(RoomCode rc)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                if (db.RoomCodes.ToList().Find(w => w.RoomCode1 == rc.RoomCode1) == null)
                {
                    RoomCode _rc = new RoomCode();
                    _rc.RoomCode1 = rc.RoomCode1;
                    _rc.Status = rc.Status;
                    _rc.CreateDate = DateTime.Now;
                    _rc.Use = rc.Use;
                    db.RoomCodes.Add(_rc);
                    db.SaveChanges();
                    return true;
                }
                else return false;
            }
        }
        public RoomCode getRoomCodeByRoomCode(string RoomCode)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.RoomCodes.Where(r=> r.RoomCode1== RoomCode).FirstOrDefault();
            }
        }
        public bool UpdateStatusRoomCode(RoomCode roomCode)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                RoomCode _rc = db.RoomCodes.Where(r=>r.RoomCode1== roomCode.RoomCode1).FirstOrDefault();
                _rc.RoomCode1 = roomCode.RoomCode1;
                _rc.Status = roomCode.Status;
                _rc.CreateDate = _rc.CreateDate;
                db.SaveChanges();
                return true; 
            }
        }

        public bool UpdateUseRoomCode(string RoomCode)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                RoomCode _rc = db.RoomCodes.Where(r => r.RoomCode1 == RoomCode).FirstOrDefault();
                _rc.Use = true;
                db.SaveChanges();
                return true;
            }
        }
        public List<ReviveRoom> getReviveRoomByRoomCode(string RoomCode)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                db.Configuration.ProxyCreationEnabled = false;
                return db.ReviveRooms.Where(r=>r.RoomCode == RoomCode).ToList();
            }
        }

        public RoomCode getRoomCode1()
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                db.Configuration.ProxyCreationEnabled = false;
                return db.RoomCodes.FirstOrDefault();
            }
        }
        public bool addNewProvideOrder(ProvideOrder po)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                db.Configuration.ProxyCreationEnabled = false;
                ProvideOrder pr = new ProvideOrder();
                pr.UserIdGive = po.UserIdGive;
                pr.UserIdGet = po.UserIdGet;
                pr.Status = po.Status;
                pr.OrderId = po.OrderId;
                pr.Amount = po.Amount;
                pr.RoomCode = po.RoomCode;
                db.ProvideOrders.Add(pr);
                db.SaveChanges();
                return true;
            }
        }
        public WaitingProvideRoom getWaitingProvideRoomById(int Id)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.WaitingProvideRooms.Where(w=>w.Id == Id).FirstOrDefault();
            }
        }
        public void deleteWaitingProvide(int Id)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                if (db.WaitingProvideRooms.Where(w => w.Id == Id).FirstOrDefault() != null)
                {
                    db.WaitingProvideRooms.Remove(db.WaitingProvideRooms.Where(w => w.Id == Id).FirstOrDefault());
                    db.SaveChanges();
                }
                
            }
        }
        public WaitingReceivedRoom getWaitingReviveRoomById(int Id)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                return db.WaitingReceivedRooms.Where(w => w.Id == Id).FirstOrDefault();
            }
        }
        public void deleteWaitingRevieRoom(int Id)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                if (db.WaitingReceivedRooms.Where(w => w.Id == Id).FirstOrDefault() != null)
                {
                    db.WaitingReceivedRooms.Remove(db.WaitingReceivedRooms.Where(w => w.Id == Id).FirstOrDefault());
                    db.SaveChanges();
                }
            }
        }
        public List<WaitingProvideRoom> getListWaitingProvideRoom()
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                DateTime date = DateTime.Now;
                date = date.AddDays(-5);
                return db.WaitingProvideRooms.OrderBy(w=>w.CreateDate).ToList();
            }
        }
        public List<WaitingReceivedRoom> getListWaitingReceivedRoom()
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                DateTime date = DateTime.Now;
                DateTime date1 = date.AddDays(-4);
                //DateTime date2 = date.AddDays(-1);
                //var result = from wait in db.WaitingReceivedRooms.Where(w => w.CreateDate.Value <= date1)
                //             join o in db.Orders.Where(o => o.Date.Value >= date2.Date && o.Action == true) on wait.UserId equals o.UserId
                //             select wait;
                var result = db.WaitingReceivedRooms.Where(w=>w.CreateDate.Value<=date1).ToList();
                var listWaitingRe = db.WaitingReceivedRooms.Where(w => w.Action == true).ToList();
                //result.ToList().AddRange(listWaitingRe);
                var rs = result.ToList().Concat(listWaitingRe).ToList();
                return rs;
            }
        }
        public bool checkOrderInProvideOrder(int OrderId)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                if (db.ProvideOrders.Where(w => w.OrderId == OrderId).FirstOrDefault() == null)
                    return false;
                else
                    return true;
                
            }
        }
        public bool updateWaitingReceivedRoom(int Id)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                var wait = db.WaitingReceivedRooms.Where(w => w.Id == Id).FirstOrDefault();
                if (wait != null)
                {
                    wait.Action = true;
                    db.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public void updateProvideOrder(int userId, int orderId, string roomCode)
        {
            string connection = DuAnDaCapEntities.getDomain();
            using (var db = DuAnDaCapEntities.initDuAnDaCapEntities(connection))
            {
                
            }
        }
    }
    public class OrderUnactive
    {
        public int OrderId { get; set; }
        public int UserId { get; set; }
        public string RoomCode { get; set; }
    }
}
